// miniprogram/group_admin/pages/confirmOrder/confirmOrder.js
// 获取api
const api = require("../../../utils/api.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    "classShow":false,
    "selectIt":false,
    "orderTitle":"",
    "loadList":"",
    "loadSystemId":"",
    "chooseMonth":[true,false,false,false],
  },

  // 用户点击确认购买
  confirmBuy:function(){
    var loadMemberSystemId=this.data.loadSystemId;
    wx.navigateTo({
      url: '/group_admin/pages/sureOrder/sureOrder?loadMemberSystemId='+loadMemberSystemId,
    })
  },

  //点击一起购买
  together:function(){
    var that=this;
    var selectIt=!this.data.selectIt;
    var index=0;
    // 获取现在选中月份的下标
    for(var i=0;i<this.data.chooseMonth.length;i++){
      if(that.data.chooseMonth[i]){
        index=i;
      }
    }
    console.log("index---------------"+index)
    // 如果现在勾勾是ok状态 显示高频题库（所有） else 显示一种的
    if(selectIt){
      that.setData({
        "selectIt":selectIt,
        "loadList":that.data.loadAllServiceList.list[index],
        "loadSystemId":that.data.loadAllServiceList.list[index].memberSystemid
      })
    }else{
      that.setData({
        "selectIt":selectIt,
        "loadList":that.data.serviceList.list[index],
        "loadSystemId":that.data.serviceList.list[index].memberSystemid
      })
    }
  },

  // 用户选择月份的时候
  clickMonth:function(e){
    var index=e.currentTarget.dataset.index;//获取从页面传递的数据
    var that=this;
    var selectIt=this.data.selectIt;
    // 判断勾勾是否为选中状态 是就所有 else 就单一的那一种 
    if(selectIt){
      that.setData({
        "loadSystemId":this.data.loadAllServiceList.list[index].memberSystemid,
        "loadList":this.data.loadAllServiceList.list[index],
        "chooseMonth":[false,false,false,false],
      })
    }else{
      that.setData({
        "loadSystemId":this.data.serviceList.list[index].memberSystemid,
        "loadList":this.data.serviceList.list[index],
        "chooseMonth":[false,false,false,false],
      })
    }
    // 将选中的下标设置为true 方便换背景
    this.setData({
      [`chooseMonth[${index}]`]:true
    })
  },

  orderModuleAllList:async function(){
    var that=this;
    //获取订单页面的基本内容
    var resYtkUserInfo= wx.getStorageSync("USERDATA");
    var courseInfo= wx.getStorageSync("COURSEINFO");
    var params = {
        "uid": resYtkUserInfo.uid,
        "courseid": courseInfo.id,
        "market": getApp().globalData.market,
        "module": "20,21"
      };
      var url = "/commodity/getMembersByModule";

      // api.sendPosts( url,params, function(res) {
      //   that.setData({
      //     "loadAllMemberSystemId":res.list[0].memberSystemid,
      //     "loadAllServiceList":res.list[0]
      //   })  
      // })

      let res = await api.sendPostsSync(url,params)
      this.setData({
        "loadAllServiceList":res
      })  
  },

  orderModuleList:async function(){
    var that=this;
    //获取订单页面的基本内容
    var resYtkUserInfo= wx.getStorageSync("USERDATA");
    var courseInfo= wx.getStorageSync("COURSEINFO");
    var params = {
        "uid": resYtkUserInfo.uid,
        "courseid": courseInfo.id,
        "market": getApp().globalData.market,
        "module": that.data.moduleType
      };
      var url = "/commodity/getMembersByModule";

      // api.sendPosts( url,params, function(res) {
      //   that.setData({
      //     "serviceList":res,
      //     "loadMemberSystemId":res.list[0].memberSystemid,
      //     "loadServiceList":res.list[0],
      //     "loadList":res.list[0],
      //     "loadSystemId":res.list[0].memberSystemid,
      //   })    
      //   if(res.list[0].Title=="高频考点"){
      //     that.setData({
      //       "flagtitle":"高频易错"
      //     })
  
      //   }else if(res.list[0].Title=="高频易错"){
      //     that.setData({
      //       "flagtitle":"高频考点"
      //     })
      //   }  
      // });

      let res = await api.sendPostsSync(url,params)
      this.setData({
        "serviceList":res,
        "loadMemberSystemId":res.list[0].memberSystemid,
        "loadServiceList":res.list[0],
        "loadList":res.list[0],
        "loadSystemId":res.list[0].memberSystemid,
      }) 
      if(res.list[0].Title=="高频考点"){
        that.setData({
          "flagtitle":"高频易错"
        })

      }else if(res.list[0].Title=="高频易错"){
        that.setData({
          "flagtitle":"高频考点"
        })
      }   

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    var moduleType;
    var MODULELIST=wx.getStorageSync('MODULELIST');
    var flagClass=options.flagClass;
    if(flagClass==1){
      that.setData({
        "classShow":true
      })
      moduleType=options.module;
    }else{
      moduleType=MODULELIST.module;
    }
    this.setData({
      "moduleType":moduleType,
      "flagClass":flagClass
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that=this;
     // 折扣 
     this.orderModuleList(); 

     if(this.data.flagClass==1){
       that.orderModuleAllList();
       that.setData({
        "selectIt":false,
        "chooseMonth":[true,false,false,false],
       })
     }
},



  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})